package com.woniuxy.qiantai.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.qiantai.entity.Cartitem;
import com.woniuxy.qiantai.entity.User;
import com.woniuxy.qiantai.service.CartitemService;
import com.woniuxy.qiantai.service.OrderService;
import com.woniuxy.qiantai.service.UserService;
import com.woniuxy.qiantai.utils.CookieUtils;
import com.woniuxy.qiantai.utils.JwtUtils;
import com.woniuxy.qiantai.vo.OrderPreview;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@RestController
@RequestMapping("/cartitem")
public class CartitemController {
    @Autowired
    UserService userService;
    @Autowired
    CartitemService cartitemService;
    @Autowired
    OrderService orderService;
    @Autowired
    RedisTemplate<String, Object> stringObjectRedisTemplate;


    /**
     * 添加商品到购物车功能
     *
     * @param productId 商品id
     * @param request   HttpServletRequest 对象
     * @return "OK"表示操作完成
     */
    @RequestMapping("addToCart")
    public String addToCart(Integer productId, HttpServletRequest request) {
    User user = getCurentUser(request);

        cartitemService.addToCart(user.getId(), productId);
        return "ok";
    }

    /**
     * //分页查询购物车功能
     *
     * @param request     HttpServletRequest 对象
     * @param pageSize    分页查询显示数量
     * @param currentPage 当前页码
     * @return Page<Cartitem> 对象
     */
    @RequestMapping("selectPage")
    public Page<Cartitem> selectPage(HttpServletRequest request, Integer pageSize, Integer currentPage) {
        User curentUser = getCurentUser(request);
       /* User curentUser = new User();
        curentUser.setId(1);
*/
        Page<Cartitem> cartitemPage = cartitemService.selectPage(curentUser.getId(), pageSize, currentPage);
        return cartitemPage;
    }

    /**
     * 根据选择的商品 计算总价
     *
     * @param productIds 商品productId数组
     * @param request    HttpServletRequest 对象
     * @return 商品总价
     */
    @RequestMapping("calculateTotalPrice")
    public BigDecimal calculateTotalPrice(@RequestParam(value = "productIds[]") Integer[] productIds, HttpServletRequest request) {
       User curentUser = getCurentUser(request);
       /* User curentUser = new User();
        curentUser.setId(1);*/
        System.out.println("===============");
        BigDecimal totalPrice = cartitemService.calculateTotalPrice(productIds, curentUser.getId());
        return totalPrice;
    }

    /**
     * 加减购物车商品的数量
     *
     * @param productId 商品productId
     * @param request   HttpServletRequest 对象
     * @param count     商品修改后的数量
     * @return
     */
    @RequestMapping("updateCartItemCount")
    public String updateCartItemCount(Integer productId, HttpServletRequest request, Integer count) {
        User curentUser = getCurentUser(request);
        /*User curentUser = new User();
        curentUser.setId(1);*/
        cartitemService.updateCount(productId, curentUser.getId(), count);
        return "ok";
    }

    /**
     * 生成预览订单
     *
     * @param productIds 商品productId数组
     * @param request    HttpServletRequest 对象
     * @return 预览订单对象：包含了购物车项和总价
     */
    @RequestMapping("createOrderPreview")
    public OrderPreview createOrderPreview(@RequestParam(value = "productIds[]") Integer[] productIds, HttpServletRequest request) {
        User curentUser = getCurentUser(request);
        /*User curentUser = new User();
        curentUser.setId(1);*/
        OrderPreview orderPreview = cartitemService.createOrderPreview(productIds, curentUser.getId());
        return orderPreview;
    }


    private User getCurentUser(HttpServletRequest request) {
        //通过请求拿到cookie中的token 再从token中获取用户名 再根据用户名查找出user对象
        String token = CookieUtils.getUserTokenFromCookie(request);
        String name = JwtUtils.getAccount(token);
        User userByUserName = userService.getUserByUserName(name);
        return userByUserName;
    }


    //订单页面去下单时发死信
    //@RequestMapping("/pay")
    //public void pay(Integer orderId){
        //把redis中商品移除掉
        //请求支付
        //-- 发送订单编号到队列中
       // orderService.sendMessageToDelayQueue(orderId);

        //支付成功后返回订单页面
   // }

}

